package br.com.erpcortume.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.erpcortume.beans.Fornecedor;
import br.com.erpcortume.services.FornecedorService;

@Controller
@RequestMapping(value = "/fornecedor")
public class FornecedorController {
	
	private static final String REDIRECT_LISTAR = "redirect:/fornecedor/listar";
	private static final String CONTEXT_PAGE = "pages";
	
	
	
	@Autowired
	private FornecedorService fornecedorService;
	
	@RequestMapping(value = "/novo")
	public String novo(){
		return CONTEXT_PAGE+"/fornecedor/fornecedor";
	}
	
	
	@RequestMapping(value = "/salvar", method = RequestMethod.POST)
	public String salvar(Fornecedor fornecedor){
		fornecedorService.salvar(fornecedor);
		return REDIRECT_LISTAR;
	}
	
	
	@RequestMapping(value = "listar")
	public ModelAndView listar(){
		List<Fornecedor> fornecedores = fornecedorService.listar();
		ModelAndView mav =  new ModelAndView(CONTEXT_PAGE+"/fornecedor/lista");
		mav.addObject(fornecedores);
		return mav;
	}
	
	
	
	@RequestMapping(value = "/toEdit/{id}", method = RequestMethod.GET)
	public ModelAndView toEdit(@PathVariable int id){
		
		Fornecedor fornecedor = fornecedorService.findById(id);
		ModelAndView modelAndView = new ModelAndView(CONTEXT_PAGE+"/fornecedor/fornecedor");
		modelAndView.addObject(fornecedor);
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/excluir/{id}")
	public String excluir(@PathVariable int id){
		
		fornecedorService.excluir(id);
		
		return REDIRECT_LISTAR;
	}

}
